package edu.java.homework.hw03.ipj06;

import java.util.Scanner;

/**
 * Calculates the sum of the elements in every column of the matrix
 * @author Tsvetan Vasilev
 *
 */
public class Exer02 {

    /**
     * tests faithfullness of the task
     * @param args
     */
    public static void main(String[] args) {
	double[][] array;
	int dimention = 0;

	Scanner input = new Scanner(System.in);
	System.out.println("Enter the dimention of the matrix.");

	dimention = input.nextInt();
	System.out.printf("N:\t%d\n", dimention);
	
	if (dimention < 2) {
	    throw new IllegalArgumentException(
		    "Incorrect input of dimention of the matrix ");
	}
	array= new double[dimention][dimention];
	inputMatrix(array);
	System.out.println("The matrix: ");
	outputMatrix(array);
	System.out.println("\nThe count of elements in columns are: ");
	printCountElems(array);
	
	input.close();
    }

    public static void printCountElems(double[][] array){
	// TODO: verify input parameter
	double sum=0;
	for(int i=0;i<array.length;i++){
	    sum=0;
	    for(int j=0;j<array.length;j++){
		sum+=array[j][i];
	    }
	    System.out.println("column[" + i + "]= " + sum);
	}
    }
    
    
    
    
    

    /**
     * create and initialize a matrix
     */
    public static void inputMatrix(double[][]array) {
	// TODO: verify input parameter
	Scanner input = new Scanner(System.in);
	
	for(int i=0;i<array.length;i++){
	    for(int j=0;j<array.length;j++){
		System.out.printf("arr[%d][%d]= ",i,j);
		array[i][j]=input.nextDouble();
		System.out.println();
	    }  
	}
	input.close();
    }
    
    /**
     * print a matrix
     */
    public static void outputMatrix(double[][]array){
	// TODO: verify input parameter

//	1st easy way of printing --> using API method Arrays.deepToString
//	System.out.println(Arrays.deepToString(array));
	
//	2nd way --> my realisation
	for(int i=0;i<array.length;i++){
	    for(int j=0; j<array.length;j++){
		System.out.printf("%.1f\t",array[i][j]);
	    }
	    System.out.println();
	}
    }
	
}
